package nl.moviefan.site.controller;

import java.security.Principal;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import nl.moviefan.site.model.Artikel;
import nl.moviefan.site.model.Beoordeling;
import nl.moviefan.site.model.MoviefanUser;
import nl.moviefan.site.service.ArtikelService;
import nl.moviefan.site.service.BeoordelingService;
import nl.moviefan.site.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

/**
 * Handler for mapping all /beoordeling uri
 */
@Controller
@RequestMapping("/beoordeling")
public class BeoordelingController {

    @Autowired
    BeoordelingService beoordelingservice;

    @Autowired
    UserService userservice;

    @Autowired
    ArtikelService artikelService;

    /**
     * Methode haalt alle recensies van ingelogde gebruiker op.
     * @param principal
     * @return 
     */
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public ModelAndView getAlleRecensiesGebruiker(Principal principal) {
        String currentUserName = principal.getName();
        MoviefanUser mu = userservice.findByUsername(currentUserName);
        List<Beoordeling> beoordelingen = beoordelingservice.getAllBeoordelingenByUser(mu);

        ModelAndView mav = new ModelAndView("beoordeling/index");
        mav.addObject("beoordelingen", beoordelingen);
        return mav;
    }

    /**
     * Methode haalt nieuw recensieformulier op met aangegeven artikelid.
     * @param artikelid
     * @param principal
     * @return 
     */
    @RequestMapping(value = "/create/{artikelid}", method = RequestMethod.GET)
    public ModelAndView getRecensieForm(@PathVariable long artikelid, Principal principal) {
        ModelAndView modelandview = new ModelAndView("beoordeling/create");
        Artikel artikel = null;
        try {
            artikel = artikelService.findByArtikelID(artikelid);
        } catch (Exception ex) {
            Logger.getLogger(BeoordelingController.class.getName()).log(Level.SEVERE, null, ex);
        }
        modelandview.addObject("artikel", artikel);
        modelandview.addObject("beoordeling", new Beoordeling());
        return modelandview;
    }

    /**
     * Methode vangt POST van URI van aangemaakte recensie.
     * @param beoordeling
     * @param artikelid
     * @param principal
     * @param bindingResult
     * @param model
     * @return 
     */
    @RequestMapping(value = "/create/{artikelid}", method = RequestMethod.POST)
    public String createRecensie(@ModelAttribute("beoordeling") Beoordeling beoordeling, @PathVariable long artikelid, Principal principal, BindingResult bindingResult, ModelAndView model) {
        String result = "beoordeling/fout";
        Artikel artikel = null;
        MoviefanUser moviefanuser = userservice.findByUsername(principal.getName());
        try {
            artikel = artikelService.findByArtikelID(artikelid);
        } catch (Exception ex) {
            Logger.getLogger(BeoordelingController.class.getName()).log(Level.SEVERE, null, ex);
        }
        beoordeling.setArtikel(artikel);
        beoordeling.setMoviefanUser(moviefanuser);
        Beoordeling beo = beoordelingservice.saveBeoordeling(beoordeling);

        if (beo != null) {
            result = "redirect:/beoordeling/index";
        }
        return result;
    }

}
